<template>
	<view class="index-container">
		<!-- 添加错误提示 -->
		<view v-if="error" class="error-tip">
			{{ error }}
		</view>
		
		<!-- 顶部搜索栏 -->
		<view class="search-bar">
			<view class="search-wrapper">
				<view class="search-box" @tap="goSearch">
					<text class="iconfont icon-search"></text>
					<text class="placeholder">陪诊</text>
				</view>
				
			</view>
		</view>
		
		<!-- 轮播图 -->
		<swiper class="banner" :indicator-dots="true" :autoplay="true" interval="3000" duration="1000"
			indicator-color="rgba(255,255,255,0.6)" indicator-active-color="#ffffff">
			<swiper-item v-for="(item, index) in banners" :key="index">
				<image :src="item.image" mode="aspectFill" />
				<view class="banner-text">
					<text class="title">{{ t(item.titleKey) }}</text>
					<text class="subtitle">{{ t(item.subtitleKey) }}</text>
				</view>
			</swiper-item>
		</swiper>
		
		<!-- 快速入口 -->
		<view class="quick-entry">
			<view 
				class="entry-item" 
				v-for="(item, index) in quickEntries" 
				:key="index" 
				@tap="navigateTo(item.path, item.type)"
			>
				<view class="icon-wrapper" :style="{ background: item.bgColor }">
					<image :src="item.icon" mode="aspectFit" />
				</view>
				<text>{{ $t(item.titleKey) }}</text>
			</view>
		</view>
		<!-- 预约通道 -->
		<view class="reserve-channel">
			<view class="channel-image">
				<image src="/static/news/news1.png" mode="aspectFill"></image>
			</view>
		</view>
		<!-- 服务团队介绍 -->
		<view class="section news-section">
			<view class="news-list">
				<view 
					class="news-item"
					v-for="(item, index) in news"
					:key="index"
					@tap="viewNews(item)"
				>
					<view class="news-image">
						<image :src="item.image" mode="aspectFill" />
						<view class="overlay"></view>
					</view>
					<view class="news-info">
						<text class="title">{{ $t(item.titleKey) }}</text>
						<view class="meta">
							<text class="tag" v-if="item.tagKey">{{ $t(item.tagKey) }}</text>
							<text class="date">{{ item.date }}</text>
						</view>
					</view>
				</view>
			</view>
		</view>
		
		<!-- 底部图片 -->
		<view class="bottom">
			<image src="/static/bottom-img.png" mode="aspectFill"></image>
		</view>
		
	</view>
</template>

<script setup>
import { ref, onMounted, getCurrentInstance } from 'vue'
import LanguageSwitch from '@/components/language-switch'
import { useI18n } from 'vue-i18n'

const { t } = useI18n()
const { proxy } = getCurrentInstance()
const error = ref('')

const banners = ref([
	{
		image: '/static/banner/banner1.png',
		titleKey: 'index.banner.smartMedical',
		subtitleKey: 'index.banner.convenientService'
	},
	{
		image: '/static/banner/banner2.png',
		titleKey: 'index.banner.specializedMedical',
		subtitleKey: 'index.banner.crossBorder'
	}
])

const quickEntries = ref([
	{
		titleKey: 'index.quickEntry.appointment',
		icon: '/static/icons/appointment.png',
		path: '/pages/appointment/index',
		type: 'tab',
		bgColor: 'linear-gradient(135deg, #67AFAB, #89C4C1)'
	},
	{
		titleKey: 'index.quickEntry.vaccine',
		icon: '/static/icons/vaccine.png',
		path: '/pages/vaccine/index',
		bgColor: 'linear-gradient(135deg, #FF9B6A, #FF6B8B)'
	},
	{
		titleKey: 'index.quickEntry.records',
		icon: '/static/icons/record.png',
		path: '/pages/records/medical',
		bgColor: 'linear-gradient(135deg, #4DCEA5, #2FB4AE)'
	},
	{
		titleKey: 'index.quickEntry.department',
		icon: '/static/icons/department.png',
		path: '/pages/department/guide',
		bgColor: 'linear-gradient(135deg, #747CF9, #9B7CF9)'
	}
])


const news = ref([
	{
		titleKey: 'index.news.cooperation',
		date: '2024-03-19',
		image: '/static/news/news2.png'
	}
])

const featuredServices = ref([
	{
		titleKey: 'index.featured.tcm',
		descKey: 'index.featured.tcmDesc',
		image: '/static/featured/tcm.png',
		path: '/pages/featured/tcm'
	},
	{
		titleKey: 'index.featured.crossBorder',
		descKey: 'index.featured.crossBorderDesc',
		image: '/static/featured/cross-border.png',
		path: '/pages/featured/cross-border'
	},
	{
		titleKey: 'index.featured.expert',
		descKey: 'index.featured.expertDesc',
		image: '/static/featured/expert.png',
		path: '/pages/featured/expert'
	}
])

const navigateTo = (path, type) => {
	if (type === 'tab') {
		uni.reLaunch({
			url: path,
			fail: (err) => {
				console.error('reLaunch fail:', err)
				uni.switchTab({
					url: path,
					fail: (err2) => {
						console.error('switchTab fail:', err2)
						uni.navigateTo({
							url: path,
							fail: (err3) => {
								console.error('navigateTo fail:', err3)
							}
						})
					}
				})
			}
		})
	} else {
		uni.navigateTo({
			url: path,
			fail: (err) => {
				console.error('navigateTo fail:', err)
			}
		})
	}
}

const viewNews = (news) => {
	uni.navigateTo({
		url: `/pages/news/detail?id=${news.id}`
	})
}

// 跳转到搜索页
const goSearch = () => {
	uni.navigateTo({
		url: '/pages/search/index'
	})
}

// 查看特色医疗
const viewFeatured = (item) => {
	if (item.path) {
		uni.navigateTo({
			url: item.path,
			fail: (err) => {
				console.error('特色医疗跳转失败:', err)
				uni.showToast({
					title: '该功能即将上线',
					icon: 'none'
				})
			}
		})
	} else {
		uni.showToast({
			title: '该功能即将上线',
			icon: 'none'
		})
	}
}

onMounted(() => {
	try {
		console.log('页面加载')
		console.log('当前语言:', proxy.$i18n.locale)
		console.log('语言包:', proxy.$i18n.messages)
	} catch (err) {
		error.value = err.message
		console.error('页面加载错误:', err)
	}
})
</script>

<style lang="scss">
	.index-container {
		min-height: 100vh;
		background: #F5F6FA;
		.search-bar {
			position: sticky;
			top: 0;
			z-index: 100;
			padding: 20rpx 30rpx;
			background: #F2F6FC;
			
			.search-wrapper {
				display: flex;
				align-items: center;
				text-align: left;
				color: #ccc;
				gap: 20rpx;
			}
			
			.search-box {
				flex: 1;
				display: flex;
				justify-content: start;
				align-items: center;
				height: 72rpx;
				background: #fff;
				border-radius: 36rpx;
				font-weight: 300;
				
				
				.icon-search {
					width: 36rpx;
					height: 36rpx;
				}
				
				input {
					flex: 1;
					font-size: 28rpx;
					color: #333;
				}
			}
		}
		.banner {
			height: 320rpx;
			padding: 10rpx 30rpx;
			position: relative;
			
			swiper-item {
				position: relative;
				
			}
			
			image {
				width: 100%;
				height: 100%;
				border-radius: 12rpx;
			}
			
			.banner-text {
				position: absolute;
				left: 40rpx;
				bottom: 40rpx;
				color: #fff;
				
				.title {
					font-size: 40rpx;
					font-weight: bold;
					margin-bottom: 10rpx;
					display: block;
					text-shadow: 0 2rpx 4rpx rgba(0,0,0,0.2);
				}
				
				.subtitle {
					font-size: 28rpx;
					opacity: 0.9;
					display: block;
					text-shadow: 0 2rpx 4rpx rgba(0,0,0,0.2);
				}
			}
		}
		
		.quick-entry {
			display: flex;
			padding: 30rpx;
			background: #F5F6FA;
			margin-bottom: 20rpx;
			
			.entry-item {
				flex: 1;
				display: flex;
				flex-direction: column;
				align-items: center;
				
				.icon-wrapper {
					width: 100rpx;
					height: 100rpx;
					border-radius: 30rpx;
					display: flex;
					align-items: center;
					justify-content: center;
					margin-bottom: 16rpx;
					box-shadow: 0 4rpx 8rpx rgba(0,0,0,0.1);
					
					image {
						width: 50rpx;
						height: 50rpx;
					}
				}
				
				text {
					font-size: 26rpx;
					color: #333;
				}
			}
		}
		
		.section {
			background: #F5F6FA;
			margin-bottom: 10rpx;
			padding: 10rpx 30rpx;
			
		}
		// 预约通道
		.reserve-channel
		{
			
			background-color: #F5F6FA;
			padding: 10rpx 30rpx;
			.channel-image{
				
				height: 140rpx;
				image {
					width: 100%;
					height: 100%;
					border-radius: 12rpx;
				}
			}
		}
		
		.news-list {
			.news-item {
				position: relative;
				border-radius: 12rpx;
				overflow: hidden;
				box-shadow: $shadow-md;
				.news-image {
					position: relative;
					height: 280rpx;
					image {
						width: 100%;
						height: 100%;
					}
					
					.overlay {
						position: absolute;
						left: 0;
						right: 0;
						bottom: 0;
						height: 50%;
						background: linear-gradient(to bottom, transparent, rgba(0,0,0,0.7));
					}
				}
				
				.news-info {
					position: absolute;
					left: 0;
					right: 0;
					bottom: 0;
					padding:10rpx 30rpx;
					z-index: 1;
					
					.title {
						font-size: 32rpx;
						color: #fff;
						font-weight: bold;
						margin-bottom: 16rpx;
						@include multi-ellipsis(2);
					}
					
					.meta {
						display: flex;
						align-items: center;
						
						.tag {
							font-size: 22rpx;
							color: #fff;
							background: $primary-color;
							padding: 4rpx 12rpx;
							border-radius: $radius-sm;
							margin-right: 16rpx;
						}
						
						.date {
							font-size: 24rpx;
							color: rgba(255,255,255,0.8);
						}
					}
				}
			}
		}
		
		.featured-list {
			margin: 0 -30rpx;
			padding: 0 30rpx;
			white-space: nowrap;
			
			.featured-item {
				display: inline-block;
				width: 400rpx;
				height: 500rpx;
				margin-right: 20rpx;
				border-radius: $radius-lg;
				overflow: hidden;
				position: relative;
				box-shadow: $shadow-md;
				
				&:last-child {
					margin-right: 0;
				}
				
				image {
					width: 100%;
					height: 100%;
				}
				
				.overlay {
					position: absolute;
					left: 0;
					right: 0;
					bottom: 0;
					height: 60%;
					background: linear-gradient(to bottom, transparent, rgba(0,0,0,0.8));
				}
				
				.featured-info {
					position: absolute;
					left: 0;
					right: 0;
					bottom: 0;
					padding: 30rpx;
					z-index: 1;
					
					.title {
						font-size: 34rpx;
						color: #fff;
						font-weight: bold;
						margin-bottom: 12rpx;
						display: block;
					}
					
					.desc {
						font-size: 26rpx;
						color: rgba(255,255,255,0.8);
						margin-bottom: 20rpx;
						display: block;
					}
					
					.btn {
						display: inline-block;
						font-size: 26rpx;
						color: #fff;
						background: rgba($primary-color, 0.3);
						border: 1rpx solid rgba(255,255,255,0.5);
						padding: 10rpx 30rpx;
						border-radius: $radius-xl;
						backdrop-filter: blur(10px);
					}
				}
				
				&:active {
					transform: scale(0.98);
				}
			}
		}
		
		.error-tip {
			padding: 20rpx;
			background: #fff;
			color: #f56c6c;
			text-align: center;
		}
		.bottom{
			
			height: 210rpx;
			image {
				width: 100%;
				height: 100%;
			}
			
		}
		
	}
</style>
